package com.yfbao.horizon.schedule.tk.sch;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
@Slf4j
@Component
public class ScheduledTaskScanner {

    @Autowired
    private ApplicationContext applicationContext;

    public void printScheduledTaskDetails() {
        String[] beanNames = applicationContext.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            Object bean = applicationContext.getBean(beanName);
            Method[] methods = bean.getClass().getDeclaredMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(Scheduled.class)) {
                    Scheduled scheduled = method.getAnnotation(Scheduled.class);
                   log.info("Scheduled Task Found:");
                    log.info("Bean: " + bean.getClass().getName());
                    log.info("Method: " + method.getName());
                    log.info("Fixed Rate: " + scheduled.fixedRate());
                    log.info("Fixed Delay: " + scheduled.fixedDelay());
                    log.info("Cron Expression: " + scheduled.cron());
                }
            }
        }
    }
}
